using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace XProject
{
    public class XEditorConfig : BaseManager<XEditorConfig>
    {


        private bool m_IsABAssetLoadMode = false;
#if UNITY_EDITOR
        public bool IsABAssetLoadMode
        {
            get
            {
                Dictionary<string, string> iniData = LoadEditorConfigIni();
                bool flag = false;
                if (!iniData.ContainsKey("AssetsLoadMode"))
                {
                    iniData.Add("AssetsLoadMode", "0");

                    SaveEditorConfigIni(iniData);
                }
                else
                {
                    if (iniData["AssetsLoadMode"] == "1")
                    {
                        flag = true;
                    }
                }
                m_IsABAssetLoadMode = flag;
                return m_IsABAssetLoadMode;
            }
            set
            {
                m_IsABAssetLoadMode = value;
                Dictionary<string, string> iniData = LoadEditorConfigIni();
                if (!iniData.ContainsKey("AssetsLoadMode"))
                {
                    iniData.Add("AssetsLoadMode", "0");
                }
                string flag;
                if (value)
                {
                    flag = "1";
                }
                else
                {
                    flag = "0";
                }
                iniData["AssetsLoadMode"] = flag;
                SaveEditorConfigIni(iniData);
            }
        }
#else
    public bool IsABAssetLoadMode = false;
#endif

        private Dictionary<string, string> LoadEditorConfigIni()
        {
            string filePath = string.Format("{0}/../{1}", Application.dataPath, "EditorConfig.ini");
            Dictionary<string, string> iniData = new Dictionary<string, string>();
            using (StreamReader reader = new StreamReader(filePath))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    line = line.Trim();
                    string[] split = line.Split('=');
                    if (!iniData.ContainsKey(split[0]))
                    {
                        iniData.Add(split[0], split[1]);
                    }
                    else
                    {
                        iniData[split[0]] = split[1];
                    }
                }
            }
            return iniData;
        }

        private void SaveEditorConfigIni(Dictionary<string, string> keyValuePairs)
        {
            string filePath = string.Format("{0}/../{1}", Application.dataPath, "EditorConfig.ini");
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                foreach (var item in keyValuePairs)
                {
                    writer.WriteLine(item.Key + "=" + item.Value);
                }

            }
        }
    }
}